package com.studyinghome.util;

import com.studyinghome.business.consts.CommonConst;

/**
 * @author Leslie (panxiang_work@163.com)
 * @website https://studyinghome.com
 * @create 2020/3/6 下午 5:54
 */
public class PasswordUtil {

    /**
     * AES 加密
     *
     * @param password 未加密的密码
     * @return
     * @throws Exception
     */
    public static String encrypt(String password) throws Exception {
        return encrypt(password, CommonConst.SALT);
    }

    /**
     * AES 解密
     *
     * @param encryptPassword 加密后的密码
     * @return
     * @throws Exception
     */
    public static String decrypt(String encryptPassword) throws Exception {
        return decrypt(encryptPassword, CommonConst.SALT);
    }

    /**
     * AES 加密
     *
     * @param password 未加密的密码
     * @param salt     盐值，默认使用用户名就可
     * @return
     * @throws Exception
     */
    private static String encrypt(String password, String salt) throws Exception {
        return AesUtil.encrypt(Md5Util.MD5(salt + CommonConst.SECURITY_KEY), password);
    }

    /**
     * AES 解密
     *
     * @param encryptPassword 加密后的密码
     * @param salt            盐值，默认使用用户名就可
     * @return
     * @throws Exception
     */
    private static String decrypt(String encryptPassword, String salt) throws Exception {
        return AesUtil.decrypt(Md5Util.MD5(salt + CommonConst.SECURITY_KEY), encryptPassword);
    }
}
